using System;
using System.Collections.Generic;
using System.Text;

namespace Deepcode.Flux.Core.Systems.Registry
{
	/// <summary>
	/// FluxRegistry mimics the Registry of windows. It allows data to be stored 
	/// in either int, string, or bool format against any key.
	/// </summary>
	public class FluxRegistry
	{
		private FluxRegistryRoot _RootKey = FluxRegistryRoot.ABSOLUTEROOT;
		private string _BranchPath = "";
		private string _FullBasePath = "";

		/// <summary>
		/// Opens the FluxRegistry from the rootkey, and optionally
		/// navigates through to branchPath.
		/// </summary>
		/// <param name="rootkey"></param>
		/// <param name="branchPath"></param>
		public FluxRegistry(FluxRegistryRoot rootkey, string branchPath)
		{
			_RootKey = rootkey;
			_BranchPath = branchPath;
		}
		/// <summary>
		/// Opens the FluxRegistry from the rootkey, and optionally
		/// navigates through to branchPath.
		/// </summary>
		/// <param name="rootkey"></param>
		public FluxRegistry(FluxRegistryRoot rootkey)
		{
			_RootKey = rootkey;
		}
		/// <summary>
		/// Sets the key specified to the value specified
		/// </summary>
		/// <param name="KeyName"></param>
		/// <param name="Value"></param>
		public void SetKey(string KeyName, int Value)
		{
			RegistryDataManager r = new RegistryDataManager();
			RegistryDataNode node = r.GetNodeRecord(GetFullBasePath(), KeyName, true);
			r.SetKeyValue(node, Value);
		}
		/// <summary>
		/// Sets the key specified to the value specified
		/// </summary>
		/// <param name="KeyName"></param>
		/// <param name="Value"></param>
		public void SetKey(string KeyName, bool Value)
		{
			RegistryDataManager r = new RegistryDataManager();
			RegistryDataNode node = r.GetNodeRecord(GetFullBasePath(), KeyName, true);
			r.SetKeyValue(node, Value);
		}
		/// <summary>
		/// Sets the key specified to the value specified
		/// </summary>
		/// <param name="KeyName"></param>
		/// <param name="Value"></param>
		public void SetKey(string KeyName, string Value)
		{
			RegistryDataManager r = new RegistryDataManager();
			RegistryDataNode node = r.GetNodeRecord(GetFullBasePath(), KeyName, true);
			r.SetKeyValue(node, Value);
		}
		/// <summary>
		/// Deletes the key specified and any child nodes it contains
		/// </summary>
		/// <param name="KeyName"></param>
		public void DeleteKey(string KeyName)
		{
			RegistryDataManager r = new RegistryDataManager();
			RegistryDataNode node = r.GetNodeRecord(GetFullBasePath(), KeyName, false);
			if (node != null)
				r.DeleteByID(node.PK_ID);
		}
		/// <summary>
		/// Returns the value of the key specified, or if the key does not exist
		/// or cannot be converted to the appropriate return type, returns DefaultValue
		/// </summary>
		/// <param name="KeyName"></param>
		/// <param name="DefaultValue"></param>
		/// <returns></returns>
		public int GetKey(string KeyName, int DefaultValue)
		{
			RegistryDataManager r = new RegistryDataManager();
			return r.GetValue(GetFullBasePath(), KeyName, DefaultValue);
		}
		/// <summary>
		/// Returns the value of the key specified, or if the key does not exist
		/// or cannot be converted to the appropriate return type, returns DefaultValue
		/// </summary>
		/// <param name="KeyName"></param>
		/// <param name="DefaultValue"></param>
		/// <returns></returns>
		public bool GetKey(string KeyName, bool DefaultValue)
		{
			RegistryDataManager r = new RegistryDataManager();
			return r.GetValue(GetFullBasePath(), KeyName, DefaultValue);
		}
		/// <summary>
		/// Returns the value of the key specified, or if the key does not exist
		/// or cannot be converted to the appropriate return type, returns DefaultValue
		/// </summary>
		/// <param name="KeyName"></param>
		/// <param name="DefaultValue"></param>
		/// <returns></returns>
		public string GetKey(string KeyName, string DefaultValue)
		{
			RegistryDataManager r = new RegistryDataManager();
			return r.GetValue(GetFullBasePath(), KeyName, DefaultValue);
		}
		/// <summary>
		/// Returns the full base path of the nodes
		/// </summary>
		/// <returns></returns>
		private string GetFullBasePath()
		{
			if (_FullBasePath != "") return _FullBasePath;
			string path = "";
			// GET ROOT KEY
			if (_RootKey == FluxRegistryRoot.ABSOLUTEROOT) path = "/";
			if (_RootKey == FluxRegistryRoot.CURRENT_USER)
			{
				Deepcode.Flux.Core.Services.Security.AdministrationSecuritySession user 
					= Deepcode.Flux.Core.Services.Security.AdministrationSecurity.GetSession();

				if (user == null) throw new Exception("User not logged in");
				path = "/USERS/" + user.PK_ID + "/";
			}
			// NOW, ADD BASE PATH
			if ((_BranchPath != "") && (_BranchPath != "/"))
			{
				if (_BranchPath.StartsWith("/")) _BranchPath = _BranchPath.Substring(1);
				path += _BranchPath;
				if (!path.EndsWith("/")) path += "/";
			}

			_FullBasePath = path;
			return path;
		}
	}
}
